package dbpack;

import java.io.*;
import java.util.*;

public class Dbpack {

    public static void main(String[] args) {
	try {
	    createIndex("..\\sakila\\inventory.csv", 0, ',', '\n', "windows-1251");
	} catch (Throwable t) {
	    t.printStackTrace();
	}
    }

    public static void log(String m) {
	System.out.println(new Date());
	System.out.println(m);
    }

    public static void createIndex(String fileName, int fieldNum, char fieldDelimiter, char rowDelimiter, String encoding) throws FileNotFoundException, IOException {
	File file = new File(fileName);
	log("createIndex " + fieldNum + " of " + file.getAbsolutePath());
	FileInputStream is = new FileInputStream(file);
	Reader reader = new InputStreamReader(is, encoding);
	int n;
	int far = 0;
	Vector<Integer> fars = new Vector<Integer>();
	Vector<Vector<String>> table = new Vector<Vector<String>>();
	Vector<String> row = new Vector<String>();
	StringBuffer stringBuffer = new StringBuffer();
	while ((n = reader.read()) != -1) {
	    far++;
	    if ((char) n == fieldDelimiter) {
		row.add(stringBuffer.toString().trim());
		stringBuffer = new StringBuffer();
	    } else {
		if ((char) n == rowDelimiter) {
		    row.add(stringBuffer.toString().trim());
		    stringBuffer = new StringBuffer();
		    table.add(row);
		    row = new Vector<String>();
		    fars.add(far);
		} else {
		    stringBuffer.append((char) n);
		}
	    }
	}
	reader.close();
	is.close();
	for (int i = 0; i < table.size(); i++) {
	    for (int r = 0; r < table.get(i).size(); r++) {
		System.out.print("/" + table.get(i).get(r));
	    }
	    System.out.println("\n" + fars.get(i) + ": ");
	}
    }
}
